package www.wen.com.common.lib.data.repository

import kotlinx.coroutines.CoroutineScope
import www.wen.com.common.lib.data.db.AppDbHelper
import www.wen.com.common.lib.data.network.ApiHelper
import www.wen.com.common.lib.data.entities.builtIn.RequestBodyParameter
import www.wen.com.common.lib.data.entities.builtIn.Result
import www.wen.com.common.lib.data.entities.nonIntrinsic.RecordEntity
import javax.inject.Inject
import javax.inject.Singleton

/**e
 * @author WEN
 * @Description: 管理所有接口的代理
 * @date 2020/11/2 16:15
 */
@Singleton
class AppDataManagerImpl @Inject constructor(
        private val apiHelper: ApiHelper,
        private val appDbHelper: AppDbHelper,
) : AppDataManager {


    override suspend fun <T> insertCache(key: String, cacheEntity: T): Boolean {
        return appDbHelper.insertCache(key, cacheEntity)
    }

    override suspend fun <T> findByKey(key: String): T? = appDbHelper.findByKey(key)

    override suspend fun <T> queryCache(
        key: String,
        error: Result.Error,
        successBlock: suspend (CoroutineScope, T) -> Unit,
        errorBlock: suspend (CoroutineScope, Result.Error) -> Unit
    ) = appDbHelper.queryCache(key, error, successBlock, errorBlock)

    override suspend fun requestSignBasket(bodyParameter: RequestBodyParameter): Result<RecordEntity> =
        apiHelper.requestSignBasket(bodyParameter)

}